package com.innovation.chengzhangriji.ui;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

import com.innovation.chengzhangriji.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class TimelineGalleryActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle bundle = getIntent().getExtras();
        int index = bundle.getInt("index", 0);
        String[] localUrls = bundle.getStringArray("urls");

        Log.d("index", "" + index);
        for (int i = 0; i < localUrls.length; i++) {
            Log.d("localUrls" + i, "" + localUrls[i]);

        }

        setContentView(R.layout.activity_timeline_gallery);
        Gallery gallery = (Gallery) findViewById(R.id.gallery);
        gallery.setAdapter(new ImageAdapter(this, localUrls));
        gallery.setSelection(index);
    }

    private static class ImageAdapter extends BaseAdapter {

        private final String[] IMAGE_URLS;

        private LayoutInflater inflater;

        private DisplayImageOptions options;

        ImageAdapter(Context context, String[] url) {

            IMAGE_URLS = url;

            inflater = LayoutInflater.from(context);

            options = new DisplayImageOptions.Builder()
                    .cacheInMemory(false)
                    .cacheOnDisk(true)
                    .considerExifParams(true)
                    .bitmapConfig(Bitmap.Config.RGB_565)
                    .build();

        }

        @Override
        public int getCount() {
            return IMAGE_URLS.length;
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.item_gallery_image, parent, false);
            }
            ImageView imageView = (ImageView) convertView.findViewById(R.id.image);
            ImageLoader.getInstance().displayImage("file://" + IMAGE_URLS[position], imageView, options);
            return convertView;
        }
    }
}
